package com.roger.tool.volley;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

/**
 * @author lch
 * @version Create time：2017-4-15
 * @类说明：
 */
public class ByteArrayRequest extends Request<byte[]> {

	private Listener<byte[]> mListener;

	public ByteArrayRequest(int method, String url, Listener<byte[]> pListener, ErrorListener errorListener) {
		super(method, url, errorListener);
		mListener = pListener;
	}

	public ByteArrayRequest(String url, Listener<byte[]> pListener, ErrorListener errorListener) {
		this(Method.GET, url, pListener, errorListener);
	}
	
    @Override
    protected void onFinish() {
        super.onFinish();
        mListener = null;
    }

	@Override
	protected void deliverResponse(byte[] response) {

		if(mListener != null){
			mListener.onResponse(response);
		}

	}

	@Override
	protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
		
		byte[] parsed = response.data;

		return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
	}

}
